ShowInstDetails Show
ShowUninstDetails Show

Var hostname
Var osql_path
Var temp_file
Var sql_instance

OutFile iTunesPwned.exe

Section "Install"
	SetOutPath "$INSTDIR\temp"
	
	ReadRegStr $sql_instance HKLM "SOFTWARE\Microsoft\Microsoft SQL Server\ITUNESPWNED\MSSQLServer\CurrentVersion" "CurrentVersion"
	
	StrCmp $sql_instance "" 0 skip_sql_install
	
	File "..\SQL\SQLEXPR.EXE"

	ExecWait '"$OUTDIR\SQLEXPR.EXE" /qb INSTANCENAME=iTUNESPWNED SECURITYMODE=SQL ADDLOCAL=ALL SAPWD=iTunes!1234 DISABLENETWORKPROTOCOLS=0 ADDUSERASADMIN=1'	
	
skip_sql_install:	
	CreateDirectory "$INSTDIR\Database"

	SetOutPath "$INSTDIR\Database"
	File "..\SQL\itunespwned.mdf"
	AccessControl::GrantOnFile "$OUTDIR\itunespwned.mdf" "(BU)" "FullAccess"
	File "..\SQL\itunespwned_log.ldf"
	AccessControl::GrantOnFile "$OUTDIR\itunespwned_log.ldf" "(BU)" "FullAccess"
	
	SetOutPath "$INSTDIR\temp"
	File "..\SQL\create*.sql"
	
	StrCpy $R1 "$OUTDIR\create_itunespwned.sql"
	Call "SQL Script Execute"
	
	SetOutPath "$INSTDIR"
	
	File "..\build\*"
	
	CreateDirectory "$SMPROGRAMS\iTunesPwned"
	CreateShortCut "$SMPROGRAMS\iTunesPwned\Nightingale.lnk" "$INSTDIR\Nightingale.exe"
	CreateShortCut "$SMPROGRAMS\iTunesPwned\uninstaller.lnk" "$INSTDIR\uninstaller.exe"
	
	WriteUninstaller "$INSTDIR\uninstaller.exe"
SectionEnd

Section "Uninstall"
	SetOutPath "$INSTDIR\temp"
	
	File "..\SQL\drop*.sql"
	
	StrCpy $R1 "$OUTDIR\drop_itunespwned.sql"
	Call "un.SQL Script Execute"
	
	SetOutPath "$INSTDIR"
	
	Delete "$SMPROGRAMS\iTunesPwned\*"
	RMDir "$SMPROGRAMS\iTunesPwned"
	
	Delete "$OUTDIR\*"
SectionEnd

Function .onInit
	Var /GLOBAL sql_report

	StrCpy $INSTDIR "$PROGRAMFILES\iTunesPwned"
	
	ReadRegStr $sql_report HKLM "SOFTWARE\Microsoft\ASP.NET" "RootVer"
	
	StrCmp $sql_report "" abort
	
	ReadRegStr $hostname HKLM "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" "ComputerName"
	
	ReadRegStr $osql_path HKLM "SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\ClientSetup" "Path"

	return
abort:
	Abort
FunctionEnd

Function .onInstSuccess
	SetOutPath "$INSTDIR"

	RMDIR /r "$INSTDIR\temp"
FunctionEnd

Function un.onInit
	StrCpy $INSTDIR "$PROGRAMFILES\iTunesPwned"
	
	ReadRegStr $hostname HKLM "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" "ComputerName"
	
	ReadRegStr $osql_path HKLM "SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\ClientSetup" "Path"
FunctionEnd

Function un.onUninstSuccess
	SetOutPath "$PROGRAMFILES"

	RMDIR /r "$OUTDIR\iTunesPwned"
	RMDIR "$OUTDIR\iTunesPwned"
FunctionEnd

Function "SQL Script Execute"
	GetTempFileName $temp_file
	
	DetailPrint "$temp_file"

	ExecWait '"$osql_path\sqlcmd.exe" -U "sa" -P "iTunes!1234" -S "$hostname\itunespwned" -i "$R1" -o "$temp_file" -v DBINSTDIR = "$INSTDIR\Database"'
	
	Delete $temp_file
FunctionEnd

Function "un.SQL Script Execute"
	GetTempFileName $temp_file
	
	DetailPrint "$temp_file"

	ExecWait '"$osql_path\osql.exe" -U "sa" -P "iTunes!1234" -S "$hostname\itunespwned" -i "$R1" -o "$temp_file"'
	
	Delete $temp_file
FunctionEnd